<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of write_to_afni</title>
  <meta name="keywords" content="write_to_afni">
  <meta name="description" content="Writes an object to a file, using the header info from a specified file.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<!-- menu.html . -->
<h1>write_to_afni
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>Writes an object to a file, using the header info from a specified file.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function [err] = write_to_afni(subj, objtype, objname, sample_filename, varargin) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Writes an object to a file, using the header info from a specified file.

 [ERR] = WRITE_TO_AFNI(SUBJ, OBJTYPE, OBJNAME, SAMPLE_FILENAME, [ARGS])

 Writes an object in subject structure SUBJ, of type OBJTYPE to a
 file. OBJNAME specifies the object, and must be an object name, a
 group name, or a cell array of object names. SAMPLE_FILENAME must
 currently be included to steal HEAD info from. This should
 ideally correspond to the data. If you can choose a SAMPLE_FILENAME that
 took part in the creation of the object, do so. If a pattern was
 created by concatenating multiple files, it makes no difference
 which is specified by SAMPLE_FILENAME. Currently, the difficulties in
 specifying a mask BRIK when writing a pattern OBJNAME are
 minimal (ie., mislabeling the type of the object as FIM, not ORIG).

 Optionally, the ARGS structure can contain the following info:
 - output_filename : the prefix of the BRIK to be saved.  This can
   be a filename, a group prefix, or a cell array of object names,
   and must correspond to the form of OBJNAME. If this is left
   out, the prefix of the BRIK will be OBJNAME.
 - runs : a selector name that specifies run info. If this is
   included, a different BRIK will be written for each run of
   data. OBJNAME must, in this case, be a single object. If runs
   is not included, a single file will be created for each object.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="find_group.html" class="code" title="function [matches] = find_group(subj,objtype,groupname)">find_group</a>	Returns a list of names of objects from this group</li><li><a href="get_mat.html" class="code" title="function [mat] = get_mat(subj,objtype,objname)">get_mat</a>	Returns the MAT field of the object</li><li><a href="get_objfield.html" class="code" title="function [val] = get_objfield(subj,objtype,objname,fieldname)">get_objfield</a>	Retrieves a field from an object</li><li><a href="propval.html" class="code" title="function [combined_struct user_struct undefaulted_struct] = propval(user_propvals,defaults_struct)">propval</a>	This deals with property/value pairs of optional arguments.</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [err] = write_to_afni(subj, objtype, objname, sample_filename, varargin)</a>
0002 
0003 <span class="comment">% Writes an object to a file, using the header info from a specified file.</span>
0004 <span class="comment">%</span>
0005 <span class="comment">% [ERR] = WRITE_TO_AFNI(SUBJ, OBJTYPE, OBJNAME, SAMPLE_FILENAME, [ARGS])</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% Writes an object in subject structure SUBJ, of type OBJTYPE to a</span>
0008 <span class="comment">% file. OBJNAME specifies the object, and must be an object name, a</span>
0009 <span class="comment">% group name, or a cell array of object names. SAMPLE_FILENAME must</span>
0010 <span class="comment">% currently be included to steal HEAD info from. This should</span>
0011 <span class="comment">% ideally correspond to the data. If you can choose a SAMPLE_FILENAME that</span>
0012 <span class="comment">% took part in the creation of the object, do so. If a pattern was</span>
0013 <span class="comment">% created by concatenating multiple files, it makes no difference</span>
0014 <span class="comment">% which is specified by SAMPLE_FILENAME. Currently, the difficulties in</span>
0015 <span class="comment">% specifying a mask BRIK when writing a pattern OBJNAME are</span>
0016 <span class="comment">% minimal (ie., mislabeling the type of the object as FIM, not ORIG).</span>
0017 <span class="comment">%</span>
0018 <span class="comment">% Optionally, the ARGS structure can contain the following info:</span>
0019 <span class="comment">% - output_filename : the prefix of the BRIK to be saved.  This can</span>
0020 <span class="comment">%   be a filename, a group prefix, or a cell array of object names,</span>
0021 <span class="comment">%   and must correspond to the form of OBJNAME. If this is left</span>
0022 <span class="comment">%   out, the prefix of the BRIK will be OBJNAME.</span>
0023 <span class="comment">% - runs : a selector name that specifies run info. If this is</span>
0024 <span class="comment">%   included, a different BRIK will be written for each run of</span>
0025 <span class="comment">%   data. OBJNAME must, in this case, be a single object. If runs</span>
0026 <span class="comment">%   is not included, a single file will be created for each object.</span>
0027 
0028 <span class="comment">% This is part of the Princeton MVPA toolbox, released under the</span>
0029 <span class="comment">% GPL. See http://www.csbmb.princeton.edu/mvpa for more</span>
0030 <span class="comment">% information.</span>
0031 
0032 
0033 <span class="comment">% Interpret OBJNAME</span>
0034 <span class="keyword">if</span> ~iscell(objname)
0035   grpmem = <a href="find_group.html" class="code" title="function [matches] = find_group(subj,objtype,groupname)">find_group</a>(subj,objtype,objname);
0036   <span class="keyword">if</span> ~isempty(grpmem)
0037     <span class="comment">% Its a group of patterns</span>
0038     objname = grpmem;
0039     
0040     default.output_filename = grpmem;
0041   <span class="keyword">else</span>
0042     <span class="comment">% It's a single pattern</span>
0043     objname = {objname};
0044     default.output_filename = objname;
0045   <span class="keyword">end</span>
0046   <span class="comment">% Otherwise, its a cell array of patterns</span>
0047 <span class="keyword">else</span>
0048   default.output_filename = objname;
0049 <span class="keyword">end</span>
0050 
0051 <span class="comment">% Process optional arguments</span>
0052 default.runs = [];
0053 args = <a href="propval.html" class="code" title="function [combined_struct user_struct undefaulted_struct] = propval(user_propvals,defaults_struct)">propval</a>(varargin,default);
0054 
0055 <span class="comment">% Error Checking: Cannot output multiple files w/ run info</span>
0056 <span class="keyword">if</span> length(unique(args.runs)) &gt; 1 &amp; length(objname) &gt; 1
0057   error(<span class="string">'Cannot pass ARGS.RUNS w/ multiple object names'</span>);
0058 <span class="keyword">end</span>
0059   
0060 <span class="comment">% Load the Brik Info</span>
0061 [err, Info] = BrikInfo(sample_filename);
0062 OrigInfo = Info;
0063 <span class="keyword">if</span> err
0064   ErrMessage = <span class="string">'Could not load BRIK info'</span>;
0065   <span class="keyword">return</span>
0066 <span class="keyword">end</span>
0067 
0068 <span class="comment">% Set the output format</span>
0069 <span class="keyword">switch</span> objtype
0070 <span class="keyword">case</span> <span class="string">'pattern'</span>
0071     Info.TypeName    = <span class="string">'float'</span>;
0072     Info.TypeBytes   = 3;
0073     Info.BRICK_TYPES = 3;
0074     ispattern = 1;
0075     ismask    = 0;
0076     Info.TYPESTRING = <span class="string">'3DIM_HEAD_FUNC'</span>;
0077 <span class="keyword">case</span> <span class="string">'mask'</span>
0078     Info.TypeName      = <span class="string">'short'</span>;
0079     Info.TypeBytes     = 1;
0080     Info.BRICK_TYPES   = 0;
0081     Info.TAXIS_NUMS    = [];
0082     Info.TAXIS_OFFSETS = [];
0083     ispattern = 0;
0084     ismask    = 1;
0085 <span class="keyword">end</span>
0086 
0087 <span class="keyword">for</span> i=1:length(objname)
0088   
0089   cur_objname = objname{i};
0090 
0091   <span class="comment">% Load the data</span>
0092   d = <a href="get_mat.html" class="code" title="function [mat] = get_mat(subj,objtype,objname)">get_mat</a>(subj,objtype,cur_objname);
0093   dDim = size(d);
0094   
0095   <span class="comment">% Determine the runs to be outputed</span>
0096   <span class="keyword">if</span> ispattern
0097     <span class="keyword">if</span> isempty(args.runs)
0098       runs = ones(1,dDim(2));
0099       <span class="keyword">if</span> prod(dDim) &gt; 1000000
0100    warning([<span class="string">'Your trying to save a mighty large file... There'</span> <span class="keyword">...</span>
0101        <span class="string">' may be a memory contraint'</span>]);
0102       <span class="keyword">end</span>
0103       
0104     <span class="keyword">else</span>
0105       runs = <a href="get_mat.html" class="code" title="function [mat] = get_mat(subj,objtype,objname)">get_mat</a>(subj,<span class="string">'selector'</span>,args.runs);
0106       <span class="keyword">if</span> length(runs) ~= dDim(2);
0107    error(<span class="string">'RUNS selector does not match dataset length'</span>);
0108       <span class="keyword">end</span>
0109     <span class="keyword">end</span>
0110   <span class="keyword">else</span>
0111     runs = 1;
0112   <span class="keyword">end</span>
0113   
0114   <span class="keyword">for</span> j=unique(runs(find(runs)))
0115   
0116     <span class="keyword">if</span> ispattern
0117       <span class="comment">% Load masked_by mask in order to populate a 3D matrix</span>
0118       m = <a href="get_mat.html" class="code" title="function [mat] = get_mat(subj,objtype,objname)">get_mat</a>(subj,<span class="string">'mask'</span>,<a href="get_objfield.html" class="code" title="function [val] = get_objfield(subj,objtype,objname,fieldname)">get_objfield</a>(subj,<span class="string">'pattern'</span>, cur_objname,<span class="string">'masked_by'</span>));
0119       mDim = size(m);
0120         
0121       cur_TRs = find(runs == j);    
0122 
0123       M = zeros([mDim,length(cur_TRs)]);
0124       
0125       <span class="keyword">for</span> k=1:length(cur_TRs);
0126    tmp = zeros(mDim);
0127    tmp(find(m)) = d(:,k);
0128    M(:,:,:,k)   = tmp;
0129       <span class="keyword">end</span>
0130       
0131       Opt.Frames = length(cur_TRs);
0132     <span class="keyword">else</span>
0133       dDim(4) = 1;
0134       mDim    = dDim;
0135       M = d;
0136       m = d;
0137       Opt.Frames = 1;
0138     <span class="keyword">end</span>
0139     
0140     <span class="comment">% Determine output name</span>
0141     <span class="keyword">if</span> length(unique(runs(find(runs)))) &gt; 1
0142       <span class="comment">% Pattern w/ Runs</span>
0143       Opt.Prefix = [args.output_filename{i} <span class="string">'_run'</span> num2str(j)];
0144     <span class="keyword">else</span>
0145       Opt.Prefix = args.output_filename{i};
0146     <span class="keyword">end</span>    
0147     
0148     <span class="comment">% Set pattern specific Info fields</span>
0149     <span class="keyword">if</span> ispattern
0150       Info.TAXIS_NUMS = [size(M,4) 0 0];
0151     <span class="keyword">end</span>
0152     
0153     <span class="comment">% Modify the INFO header to reflect the changes</span>
0154     Info.DATASET_RANK = [3 size(M,4) 0 0 0 0 0 0];
0155     Info.BRICK_STATS = [min(reshape(M,prod(mDim),size(M,4))) max(reshape(M,prod(mDim),size(M,4)))];
0156     Info.BRICK_TYPES      = OrigInfo.BRICK_TYPES(1)      * ones(1,size(M,4));
0157     Info.BRICK_FLOAT_FACS = OrigInfo.BRICK_FLOAT_FACS(1) * ones(1,size(M, 4));
0158     
0159     <span class="comment">% Write the BRIK</span>
0160     Opt.Slices = [1:size(m,3)];
0161     Opt.Frames = [1:size(M,4)];
0162     [err, ErrMessage, Info] = WriteBrik (M, Info, Opt);
0163   <span class="keyword">end</span>
0164 <span class="keyword">end</span>
0165</pre></div>
<hr><address>Generated on Tue 06-Sep-2005 22:11:31 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2003</address>
</body>
</html>